package cn.neo.support.img;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.IntDef;

import java.io.File;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Arrays;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * <b>Name:</b>xx页面<br>
 * <b>Author:</b> WK（wangkun@xjzhicheng.com)<br>
 * <b>Date:</b>  2017-05-09<br>
 * <b>Describe:</b><p> PLEASE TO FIXME...</p><br>
 * <p>代码说明：（重点）<br>
 * <ol>
 * <li> write something content..</li>
 * </ol>
 * </p>
 * <b>Version:</b>V1.0<br>
 * <b>Revision History:</b>
 * <ul>
 * v1.x<li>1.write something content..。<br>2.write something content..<br>3.write something content..<br></li>
 * </ul>
 * <br>
 */
public class KCompressor {
    public static final int FIRST_STRATEGY = 1;
    public static final int THIRD_STRATEGY = 3;
    public static final int CUSTOM_STRATEGY = 4;
    public static final int JNI_STRATEGY = 5;

    private final Context context;
    private final Builder builder;

    private KCompressor(Context context, Builder builder) {
        this.context = context;
        this.builder = builder;
    }

    public static Builder with(Context context) {
        return new Builder(context);
    }


    public static class Builder {
        Context context;
        //压缩的最大尺寸
        int maxSize;
        //压缩的最大宽度
        int maxWidth;
        //压缩的最大高度
        int maxHeight;
        //缓存路径
        File cacheDir;
        File targetIMG;
        File[] targetIMGList;
        Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.JPEG;
        int strategy = KCompressor.THIRD_STRATEGY;
        //压缩监听事件
        OnCompressListener onCompressListener;
        OnMultiCompressListener onMultiCompressListener;


        public Builder(Context context) {
            this.context = context;
        }

        public Builder setTarget(File targetIMG) {
            this.targetIMG = targetIMG;
            return this;
        }

        public Builder setTarget(File[] targetIMGList) {
            this.targetIMGList = targetIMGList;
            return this;
        }

        /**
         * 自定义图片压缩格式
         */
        public Builder setCompressFormat(Bitmap.CompressFormat compressFormat) {
            this.compressFormat = compressFormat;
            return this;
        }

        /**
         * 自定义压缩模式 FIRST_GEAR、THIRD_GEAR、CUSTOM_GEAR
         */
        public Builder setStrategy(@STRATEGY int strategy) {
            this.strategy = strategy;
            return this;
        }

        public Builder setCacheDir(File cacheDir) {
            this.cacheDir = cacheDir;
            return this;
        }

        public Builder setMaxSize(int maxSize) {
            this.maxSize = maxSize;
            return this;
        }

        public Builder setMaxWidth(int maxWidth) {
            this.maxWidth = maxWidth;
            return this;
        }

        public Builder setMaxHeight(int maxHeight) {
            this.maxHeight = maxHeight;
            return this;
        }

        public Builder setListener(OnCompressListener listener) {
            this.onCompressListener = listener;
            return this;
        }

        public Builder setListener(OnMultiCompressListener listener) {
            this.onMultiCompressListener = listener;
            return this;
        }

        //执行压缩算法
        public void launch() {
            CompresserHelper compresserHelper = new CompresserHelper(this);
            if (targetIMG != null) {
                compresserHelper.singleAction(targetIMG)
                        .observeOn(AndroidSchedulers.mainThread()).doOnSubscribe(
                        new Consumer<Disposable>() {
                            @Override
                            public void accept(Disposable disposable) throws Exception {
                                onCompressListener.onStart();
                            }
                        })
                        .subscribe(new Consumer<File>() {
                            @Override
                            public void accept(File file) throws Exception {
                                onCompressListener.onSuccess(file);
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                onCompressListener.onError(throwable);
                            }
                        });
                ;
            } else {
                compresserHelper.multiAction(Arrays.asList(targetIMGList))
                        .observeOn(AndroidSchedulers.mainThread())
                        .doOnSubscribe(new Consumer<Disposable>() {
                            @Override
                            public void accept(Disposable disposable) throws Exception {
                                onMultiCompressListener.onStart();
                            }
                        })
                        .subscribe(new Consumer<List<File>>() {
                            @Override
                            public void accept(List<File> files) throws Exception {
                                onMultiCompressListener.onSuccess(files);
                            }

                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                onMultiCompressListener.onError(throwable);
                            }
                        });
                ;

            }
        }
    }


    @IntDef({FIRST_STRATEGY, THIRD_STRATEGY, CUSTOM_STRATEGY, JNI_STRATEGY})
    @Target(ElementType.PARAMETER)
    @Retention(RetentionPolicy.SOURCE)
    @interface STRATEGY {

    }
}
